Imports DotNetNuke
Imports System.Web.UI
Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Entities.Modules.Actions


Namespace OpenLight.Modules.ConfigEditor

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' The View class displays the content
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Partial Class View
        Inherits Entities.Modules.PortalModuleBase
        Implements IActionable



        Private configPath As String = HttpContext.Current _
                                        .Server.MapPath("~/web.config")

#Region "Event Handlers"

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Page_Load runs when the control is loaded
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                If Not Page.IsPostBack Then

                    If Me.UserInfo.IsSuperUser Then
                        Me.ConfigEditorTextBox.Text = _
                                              IO.File.ReadAllText(configPath)

                        Me.EditorPanel.Visible = True
                    Else


                        Me.EditorPanel.Visible = False
                        Me.StatusLabel.Text = "You must be a SuperUser to use this module."


                    End If


                End If
            Catch exc As Exception
                ProcessModuleLoadException("Web.config could not e loaded.", Me, exc, True)
            End Try
        End Sub

#End Region

        Protected Sub SaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveButton.Click

            If Me.UserInfo.IsSuperUser Then


                Try

                    If Me.CreateBackupCheckBox.Checked Then

                        Dim fileName As String = Me.BackupFileNameTextBox.Text

                        If String.IsNullOrEmpty(fileName) Then

                            fileName = "web.config.ceb"

                        Else

                            fileName &= ".ceb"

                        End If

                        IO.File.Copy(configPath, IO.Path.Combine _
                                    (Me.PortalSettings.HomeDirectoryMapPath, fileName))

                    End If

                    IO.File.WriteAllText(configPath, Me.ConfigEditorTextBox.Text)

                    Me.StatusLabel.Text = "Web.config has been updated."

                Catch ex As Exception

                    ProcessModuleLoadException("Web.config could not be updated.", Me, ex, True)

                End Try

            Else

                Me.StatusLabel.Text = "You must be a SuperUser to use this module."

            End If
        End Sub

        Public ReadOnly Property ModuleActions() As ModuleActionCollection Implements IActionable.ModuleActions
            Get
                Dim actions As New Actions.ModuleActionCollection

                actions.Add(GetNextActionID(), "Restore Config", _
                                ModuleActionType.AddContent, _
                                "", "", EditUrl(), False, _
                                SecurityAccessLevel.Host, True, False)

                Return actions


            End Get
        End Property
    End Class

End Namespace
